package com.hro.hro_app.gcm;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.hro.hro_app.R;
import com.hro.hro_app.dataController.TeacherController;
import com.hro.hro_app.main.DayRooster;
import com.hro.hro_app.model.Device;
import com.hro.hro_app.model.Teacher;
import com.hro.hro_app.util.Properties;

public class GCMIntentService extends GCMBaseIntentService {

	private static int notificationId = 0;

	@Override
	protected String[] getSenderIds(Context context) {

		String[] ids = new String[1];
		ids[0] = Properties.senderId;
		return ids;
	}

	@Override
	protected void onError(Context ctx, String id) {

		Log.i(Properties.TAG, "on error: " + id);
	}

	@Override
	protected void onMessage(Context ctx, Intent i) {

		try {

			String json = i.getStringExtra("message");
			JSONObject obj = new JSONObject(json);

			String code = (String) obj.get("code");
			boolean absent = (Boolean) obj.get("absent");

			TeacherController.setTeacherAbsent(new Teacher(code), absent);

			Handler refresh = new Handler(Looper.getMainLooper());
			refresh.post(new Runnable() {

				public void run() {

					if (DayRooster.isOnFront) {
						DayRooster.dayRooster.showLessons();
					}
				}
			});

			generateNotification(ctx, code, absent);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onRegistered(Context ctx, String id) {

		Properties.deviceID = id;

		Device device = new Device(id);
		ServerUtil.register(device);
	}

	@Override
	protected void onUnregistered(Context ctx, String id) {

		if (id == null || id.equals("")) {
			id = Properties.deviceID;
		}

		Device device = new Device(id);
		ServerUtil.unregister(device);
	}

	private static void generateNotification(Context context, String code, boolean absent) {

		String message;

		if (absent) {
			message = "Docent: " + code + " is afwezig.";
		}
		else {
			message = "Docent: " + code + " is weer aanwezig.";
		}

		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.notification_icon, message, System.currentTimeMillis());

		String title = context.getString(R.string.app_name);

		Intent notificationIntent = new Intent(context, DayRooster.class);
		// set intent so it does not start a new activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
		notification.setLatestEventInfo(context, title, message, intent);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		// Play default notification sound
		notification.defaults |= Notification.DEFAULT_SOUND;

		// Vibrate if vibrate is enabled
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notificationManager.notify(notificationId, notification);
		notificationId++;
	}
}
